#include "CubeAsset.h"


float posX, posY, posZ;
bool fired;
float original_scale_x, original_scale_y, original_scale_z;
float current_scale_x, current_scale_y, current_scale_z;

CubeAsset::CubeAsset()
  : GameAsset(
	      string("shaders/hello-gl.v.glsl")
	      , string("shaders/hello-gl.f.glsl")
	      )
{
  CubeAsset(0, 0, 0);
  posX = 0;
  posY = 0;
  posZ = 0;
  original_scale_x = 1.0;
  original_scale_y = 1.0;
  original_scale_z = 1.0;
  current_scale_x = original_scale_x;
  current_scale_y = original_scale_y;
  current_scale_z = original_scale_z;
}

CubeAsset::CubeAsset(float x, float y, float z) {
  this->li = nullptr;
  this->pos = shared_ptr<Point3>(new Point3(x, y, z));
  posX = x;
  posY = y;
  posZ = z;
  original_scale_x = 1.0;
  original_scale_y = 1.0;
  original_scale_z = 1.0;
  current_scale_x = original_scale_x;
  current_scale_y = original_scale_y;
  current_scale_z = original_scale_z;
  // A default "unit" triangular pyramid
  num_vertices = 8;
  num_triangles = 12;
  g_vertex_buffer_data = new GLfloat[num_vertices * 3] // three points per vertex
  //vertices
  //	X 	  Y		Z
  {
	  -0.5, -0.5, -0.5,//0
	  -0.5, +0.5, -0.5,//1
	  -0.5, +0.5, +0.5,//2
	  -0.5, -0.5, +0.5,//3
	  +0.5, -0.5, +0.5,//4
	  +0.5, -0.5, -0.5,//5
	  +0.5, +0.5, -0.5,//6
	  +0.5, +0.5, +0.5 //7
  };
  g_element_buffer_data = new GLushort[num_triangles * 3] // three vertices per triangle
  //triangles
  //1 2 3
  {
	0,1,2,//0
	0,2,3,//1
	0,6,1,//2
	0,5,6,//3
	0,5,4,//4
	0,4,3,//5
	7,4,5,//6
	7,5,6,//7
	7,1,6,//8
	7,2,1,//9
	7,2,3,//10
	7,3,4 //11
  };

  mv_matrix = mv_matrix.translation(Vector3(x, y, z));
  bbox.reset();
  bbox = shared_ptr<BoundingBox>(new BoundingBox(Point3(x, y, z), 1.0, 1.0, 1.0));

  make_resources();
}

CubeAsset::CubeAsset(float x, float y, float z, string file)
	: GameAsset(string("shaders/hello-gl.v.glsl"), string(file)){
	  this->li = nullptr;
	  this->pos = shared_ptr<Point3>(new Point3(x, y, z));
	  posX = x;
	  posY = y;
	  posZ = z;
	  // A default "unit" triangular pyramid
	  num_vertices = 8;
	  num_triangles = 12;
	  g_vertex_buffer_data = new GLfloat[num_vertices * 3] // three points per vertex
	  //vertices
	  //	X 	  Y		Z
	  {
		  -0.5, -0.5, -0.5,//0
		  -0.5, +0.5, -0.5,//1
		  -0.5, +0.5, +0.5,//2
		  -0.5, -0.5, +0.5,//3
		  +0.5, -0.5, +0.5,//4
		  +0.5, -0.5, -0.5,//5
		  +0.5, +0.5, -0.5,//6
		  +0.5, +0.5, +0.5 //7
	  };
	  g_element_buffer_data = new GLushort[num_triangles * 3] // three vertices per triangle
	  //triangles
	  //1 2 3
	  {
		0,1,2,//0
		0,2,3,//1
		0,6,1,//2
		0,5,6,//3
		0,5,4,//4
		0,4,3,//5
		7,4,5,//6
		7,5,6,//7
		7,1,6,//8
		7,2,1,//9
		7,2,3,//10
		7,3,4 //11
	  };

	  mv_matrix = mv_matrix.translation(Vector3(x, y, z));
	  bbox.reset();
	  bbox = shared_ptr<BoundingBox>(new BoundingBox(Point3(x, y, z), 1.0, 1.0, 1.0));

	  make_resources();
}

CubeAsset::~CubeAsset() {
  // TODO: do something nice here.
}

void CubeAsset::update() {
}

void CubeAsset::setInterpolator(shared_ptr<IInterpolator> li) {
  this->li.swap(li);
}

void CubeAsset::draw() {
  GameAsset::draw();
}

float move_speed2 = 0.2;

void CubeAsset::move(int dir){
	switch(dir){
		//forward
		case 0:
			mv_matrix = mv_matrix * Matrix4::translation(Vector3(0.0, move_speed2, 0.0));
			posY+=move_speed2;
			break;
		//left
		case 1:
			mv_matrix = mv_matrix * Matrix4::translation(Vector3(-move_speed2, 0.0, 0.0));
			posX-=move_speed2;
			break;
		//backward
		case 2:
			mv_matrix = mv_matrix * Matrix4::translation(Vector3(0.0, -move_speed2, 0.0));
			posY-=move_speed2;
			break;
		//right
		case 3:
			mv_matrix = mv_matrix * Matrix4::translation(Vector3(move_speed2, 0, 0));
			posX+=move_speed2;
			break;
		//up
		case 4:
			mv_matrix = mv_matrix * Matrix4::translation(Vector3(0.0, 0.0, move_speed2));
			posZ+=move_speed2;
			break;
		//down
		case 5:
			mv_matrix = mv_matrix * Matrix4::translation(Vector3(0, 0.0, -move_speed2));
			posZ-=move_speed2;
			break;
		default:
			mv_matrix = mv_matrix * Matrix4::translation(Vector3(0.0, 0.0, 0.0));
			posX+=0.0;
			posY+=0.0;
			posZ+=0.0;
			break;
	}
	bbox.reset();
	bbox = shared_ptr<BoundingBox>(new BoundingBox(Point3(mv_matrix.getTranslation().getX(), mv_matrix.getTranslation().getY(), mv_matrix.getTranslation().getZ()), 1.0, 1.0, 1.0));
}

void CubeAsset::scale(float x, float y, float z){
	mv_matrix *= Matrix4::scale(Vector3(x, y, z));
	current_scale_x /= current_scale_x * x;
	current_scale_y /= current_scale_y * y;
	current_scale_z /= current_scale_z * z;
	bbox.reset();
	bbox = shared_ptr<BoundingBox>(new BoundingBox(Point3(mv_matrix.getTranslation().getX(), mv_matrix.getTranslation().getY(), mv_matrix.getTranslation().getZ()), current_scale_x, current_scale_y, current_scale_z));
}

void CubeAsset::lookAt(int x, int y){
	for(int i=0;i<12;i++){
		g_vertex_buffer_data[i] * Matrix4::rotation(1.0, Vector3(1,0,0));
	}
}
